package com.dragon.app.commons.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.Properties;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.marvels.common.utils.MarvelsLogUtil;


@Component
public class IODeagonAppUtils {
	
	private static String configPropertiesPath;
	
	
	public  String getConfigPropertiesPath() {
		return configPropertiesPath;
	}
	
	@Value("${config.properties.path}")
	public  void setConfigPropertiesPath(String configPropertiesPath) {
		IODeagonAppUtils.configPropertiesPath = configPropertiesPath;
	}
	
	public static Properties getProperties(String relativePath){
		Properties prop=null;
		try {   
        	String filePath=configPropertiesPath+ File.separator+relativePath;
    		File loadFile = new File(filePath);
    		InputStream in = new FileInputStream(loadFile);
    		prop = new Properties();
    		prop.load(new InputStreamReader(in,"UTF-8"));
            prop.load(in);   
        } catch (IOException e) {   
            MarvelsLogUtil.getInstance().error("=====ERROR=====", e); 
        }   
		return prop;
	}
	
	/**
	 * 把一个流变成字符串
	 * @param is
	 * @return
	 */
	public static String convertStreamToString(InputStream is) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }
        StringBuilder sb = new StringBuilder();
        String line = null;
        try {
        	if(reader != null){
        		 while ((line = reader.readLine()) != null) {
                     sb.append(line + "\n");
                 }
        	}
        } catch (IOException e) {
            MarvelsLogUtil.getInstance().error("=====ERROR=====", e);
        } finally {
            try {
            	if(is != null) {
                is.close();
            	}
            } catch (IOException e) {
                MarvelsLogUtil.getInstance().error("=====ERROR=====", e);
            }
        }

        return sb.toString();
    }
	
	public static void main(String[] args) {
	/*	Date currentTime = new Date();
		int year = currentTime.getYear();
		int month = currentTime.getMonth();
		int day = currentTime.getDay();
		System.out.println("年 : " + year + "-----" + "月 ； " + month + "-------" + "日 : " + day);*/
		
		Calendar cal = Calendar.getInstance();
	    int day = cal.get(Calendar.DATE);
	    int month = cal.get(Calendar.MONTH) + 1;
	    int year = cal.get(Calendar.YEAR);
	    int dow = cal.get(Calendar.DAY_OF_WEEK);
	    int dom = cal.get(Calendar.DAY_OF_MONTH);
	    int doy = cal.get(Calendar.DAY_OF_YEAR);

	    System.out.println("Current Date: " + cal.getTime());
	    System.out.println("Day: " + day);
	    System.out.println("Month: " + month);
	    System.out.println("Year: " + year);
	    System.out.println("Day of Week: " + dow);
	    System.out.println("Day of Month: " + dom);
	    System.out.println("Day of Year: " + doy);
	}
	
}
